home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
Book Chapters
/
03 - Advanced Graphics
/
Example 7
/
main.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-02-28
|
5KB
|
239 lines
//
// File: main.c
//
// This file contains the code for a VERY minimal application.
// It will bring up a set an app palette, open a window and
// run a sprite demo.
//
// 2/18/95 -- Created by Mick
//
// include files
#include <profiler.h>
#include "global.h"
#include <LowMem.h> // the CodeWarrior version -- if you are using Symantec, include LoMem.h instead
#include <Palettes.h>
#include "main.h"
#include "demo.h"
// defines for this file
// global function declarations
void main( void );
// global data owned by this file
WindowPtr gMainWindow; // the window that we are drawing to
CTabHandle gAppColorTable; // the color table that we are drawing with
// local function declarations
static void initMacToolboxes( void );
static void doEventLoop( void );
static void hideMenuBar( void );
static void showMenuBar( void );
// static data
static unsigned short sMenuBarHeight; // the height of the menu bar when the program started
static RgnHandle sOriginalGrayRgn; // the gray region when the program started
// functions
//
// main -
// This is where it all begins.
//
void main( void )
{
PaletteHandle appPalette; // the palette that we will make the application palette
// startup the profiler
#if __profile__
if ( ProfilerInit( collectDetailed, bestTimeBase, 100, 10 ) != noErr )
{
Debugger();
}
#endif
// fire up the toolboxes
initMacToolboxes();
// get the color table and make it the default palette
gAppColorTable = GetCTable( kAppColorTableResID );
appPalette = NewPalette( 256, gAppColorTable, pmExplicit + pmTolerant, 0 );
SetPalette( ( WindowPtr )-1L, appPalette, kFalse );
// hide the menu bar
hideMenuBar();
// create and show the window
gMainWindow = GetNewCWindow( kMainWindowResID, ( Ptr )kNil, kWindowInFront );
ShowWindow( gMainWindow );
SetPort( gMainWindow );
// give the demo code a chance to setup
startupDemo();
// handle events until the user signals a quit
doEventLoop();
// give the demo code a chance to shutdown
shutdownDemo();
// hide and delete the window
HideWindow( gMainWindow );
DisposeWindow( gMainWindow );
// show the menu bar again
showMenuBar();
// shutdown the profiler
#if __profile__
ProfilerDump( "\pDemo.prof" );
ProfilerTerm();
#endif
}
//
// initMacToolboxes -
// Initialize all the toolboxes that we will need.
//
void initMacToolboxes( void )
{
// initialize all the toolboxes
InitGraf( &( qd.thePort ) );
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs( ( ProcPtr )kNil );
InitCursor();
// make sure that we have the enitre heap
MaxApplZone();
// allocate some extra master pointers
MoreMasters();
MoreMasters();
MoreMasters();
MoreMasters();
// set the random seed
qd.randSeed = TickCount();
}
//
// doEventLoop -
//
// Get and process events until the user signals a quit.
//
void doEventLoop( void )
{
unsigned char quitNow; // a flag that notes if the user has signaled a quit
EventRecord theEvent; // a place to put an event
quitNow = kFalse;
while( !quitNow )
{
if( WaitNextEvent( everyEvent, &theEvent, 0L, ( RgnHandle )kNil ) == kTrue )
{
switch( theEvent.what )
{
case mouseDown:
// give the demo uninteruppted time to draw
while( StillDown() )
{
doDemoFrame();
}
break;
case keyDown:
// check to see if the user has hit cmd-q (to signal a quit)
if ( ( theEvent.modifiers & cmdKey ) && ( ( theEvent.message & charCodeMask ) == 'q' ) )
{
quitNow = kTrue;
}
else
{
// send the key to the demo code
demoKey( ( unsigned char )( theEvent.message & charCodeMask ) );
}
break;
case updateEvt:
BeginUpdate( gMainWindow );
EndUpdate( gMainWindow );
default:
break;
}
}
else
{
// give the demo code some time to run
doDemoFrame();
}
}
}
//
// hideMenuBar -
//
// Hides the menu bar and adds it to the area that can be drawn
//
void hideMenuBar( void )
{
GDHandle mainScreen; // the information on the main screen
Rect mainScreenRect; // the rect that bounds the menu bar
RgnHandle mainScreenRgn; // the region of the menu bar
// record the menu bar height
sMenuBarHeight = GetMBarHeight();
// set the menu bar to no height
LMSetMBarHeight( 0 );
// save the original gray regions (so we can restore it later)
sOriginalGrayRgn = NewRgn();
CopyRgn( GetGrayRgn(), sOriginalGrayRgn );
// make sure that the entire main screen is ours to play with (no menu bars or corners)
mainScreenRgn = NewRgn();
mainScreen = GetMainDevice();
mainScreenRect = ( ( *mainScreen )->gdRect );
RectRgn( mainScreenRgn, &mainScreenRect );
UnionRgn( sOriginalGrayRgn, mainScreenRgn, GetGrayRgn() );
// draw the desktop
PaintOne( ( WindowRef )kNil, sOriginalGrayRgn );
}
//
// showMenuBar -
//
// Shows the menu bar and removes it from the drawable area.
//
void showMenuBar( void )
{
// set the menu bar to its normal height
LMSetMBarHeight( sMenuBarHeight );
// restore the original gray region
SectRgn( sOriginalGrayRgn, GetGrayRgn(), GetGrayRgn() );
// draw the menu bar
DrawMenuBar();
}